<template>
  <header class="header-nav">
    <!-- 左侧Logo和标题 -->
    <div class="header-left">
      <div class="logo">
        <img src="/favicon.ico" alt="Logo" class="logo-img" />
      </div>
      <h1 class="site-title">{{ siteTitle }}</h1>
    </div>

    <!-- 中间导航链接 -->
    <nav class="header-center">
      <ul class="nav-links">
        <li v-for="item in navItems" :key="item.path" class="nav-item">
          <router-link :to="item.path" class="nav-link" :active-class="'active'">
            {{ item.name }}
          </router-link>
        </li>
      </ul>
    </nav>

    <!-- 右侧用户菜单 -->
    <div class="header-right">
      <div class="user-menu">
        <button class="user-button" @click="toggleUserMenu">
          <span class="user-avatar">{{ userInfo.name }}</span>
          <span class="dropdown-arrow">{{ userMenuOpen ? '▲' : '▼' }}</span>
        </button>
        <div v-if="userMenuOpen" class="user-dropdown">
          <ul class="user-menu-items">
            <li v-for="menu in userMenuItems" :key="menu.key" class="user-menu-item" @click="handleMenuItem(menu.key)">
              {{ menu.label }}
            </li>
          </ul>
        </div>
      </div>
    </div>
  </header>
</template>

<script>
export default {
  name: 'HeaderNav',
  props: {
    // 网站标题
    siteTitle: {
      type: String,
      default: '我的网站'
    },
    // 导航链接配置
    navItems: {
      type: Array,
      default: () => [
        { name: '首页', path: '/' },
        { name: '关于我们', path: '/about' },
        { name: '产品中心', path: '/products' },
        { name: '服务支持', path: '/services' },
        { name: '联系我们', path: '/contact' }
      ]
    },
    // 用户信息
    userInfo: {
      type: Object,
      default: () => ({ name: '用户' })
    },
    // 用户菜单项
    userMenuItems: {
      type: Array,
      default: () => [
        { key: 'profile', label: '个人资料' },
        { key: 'settings', label: '系统设置' },
        { key: 'logout', label: '退出登录' }
      ]
    }
  },
  data() {
    return {
      userMenuOpen: false
    }
  },
  methods: {
    toggleUserMenu() {
      this.userMenuOpen = !this.userMenuOpen;
    },
    handleMenuItem(action) {
      // 触发自定义事件，父组件可以监听并处理
      this.$emit('menu-item-click', action);
      this.userMenuOpen = false;
    }
  },
  mounted() {
    // 点击页面其他地方关闭用户菜单
    document.addEventListener('click', (e) => {
      if (!this.$el.contains(e.target)) {
        this.userMenuOpen = false;
      }
    });
  },
  beforeUnmount() {
    // 清理事件监听器
    document.removeEventListener('click', () => {});
  }
}
</script>

<style scoped>
.header-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 60px;
  background-color: #ffffff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
}

/* 左侧Logo和标题 */
.header-left {
  display: flex;
  align-items: center;
}

.logo {
  margin-right: 10px;
}

.logo-img {
  height: 40px;
  width: auto;
}

.site-title {
  font-size: 20px;
  font-weight: bold;
  color: #333;
  margin: 0;
}

/* 中间导航 */
.header-center {
  flex: 1;
  display: flex;
  justify-content: center;
}

.nav-links {
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
}

.nav-item {
  margin: 0 15px;
}

.nav-link {
  text-decoration: none;
  color: #666;
  font-size: 16px;
  padding: 8px 12px;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.nav-link:hover {
  color: #007bff;
}

.nav-link.active {
  color: #007bff;
  font-weight: bold;
}

/* 右侧用户菜单 */
.header-right {
  position: relative;
}

.user-menu {
  position: relative;
}

.user-button {
  display: flex;
  align-items: center;
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 4px;
  transition: background-color 0.3s ease;
}

.user-button:hover {
  background-color: #f5f5f5;
}

.user-avatar {
  font-size: 14px;
  color: #333;
  margin-right: 5px;
}

.dropdown-arrow {
  font-size: 10px;
  color: #666;
}

.user-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 5px;
  background-color: #ffffff;
  border-radius: 4px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  min-width: 150px;
  z-index: 1001;
}

.user-menu-items {
  list-style: none;
  margin: 0;
  padding: 8px 0;
}

.user-menu-item {
  padding: 10px 20px;
  cursor: pointer;
  font-size: 14px;
  color: #333;
  transition: background-color 0.2s ease;
}

.user-menu-item:hover {
  background-color: #f5f5f5;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-nav {
    flex-direction: column;
    height: auto;
    padding: 10px;
  }
  
  .header-left,
  .header-center,
  .header-right {
    width: 100%;
    justify-content: center;
    margin-bottom: 10px;
  }
  
  .nav-links {
    flex-wrap: wrap;
    justify-content: center;
  }
  
  .nav-item {
    margin: 5px;
  }
}
</style>